<?php

class Pruebas extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $query = "
                   
                    select ah.ID_Alojamiento as id_alo, ah.ID_Habitacion as id_hab, cal_calendario.fecha as fecha_disp  
                    from
                    cal_calendario
                    inner join alojamientos_habitaciones as ah
                    on
                    cal_calendario.id_habitacion=ah.ID_Habitacion
                    where
                    fecha>'2013-01-01' and fecha<'2013-01-016'
                ";

        $rows_count = $this->db->query($query);
        $rows = $rows_count->result_array();
        $total = $rows_count->num_rows();
        $id_alojamiento = "";
        $id_habitacion = "";
        $count = 0;
        $final = array();
        $final_total = array();
        $alojamientos = array();
        $habitaciones = array();
        $fechas = array();
        
        /*a.ID_Alojamiento,
                i.Nombre,
                i.Descripcion,
                p.CountryName,
                pr.SuName,
                ci.Name,
                lo.Localidad,
                c.tarifa_normal,
                c.tarifa_oferta,
                c.cant_disponible,
                h.NombreHab **/
        
        foreach ($rows as $var)
        {
            $count++;
            if ($count == 1)
            {
                $id_alojamiento = $var['ID_Alojamiento'];
                $id_habitacion = $var['ID_Habitacion'];
            }
            array_unshift($fechas, $var['fecha_disp']);
            if ($id_habitacion != $var['id_hab'] or $count == $total)// or $count == $total
            {
                $hab= array(
                                'id_habitacion' => $id_habitacion,
                                'fechas'        => $fechas
                               );
                array_unshift($habitaciones, $hab);
                $fechas=array();
                if ($id_alojamiento != $var['id_alo'] or $count == $total)//or $count == $total
                {
                    $final = array(
                        'id_alojamiento' => $id_alojamiento,
                        'habitacion' => $habitaciones
                    );
                    array_unshift($final_total, $final);
                    $final = array();
                    $fechas = array();
                    $habitaciones = array();
                    $hab=array();
                }
                $id_habitacion = $var['id_hab'];
            }
            $id_alojamiento = $var['id_alo'];
        }

        foreach ($final_total as $var1 => $info1)
        {
            echo "id_alojamiento: " . $info1['id_alojamiento'] . "<br>";
            foreach ($info1['habitacion'] as $info2)
            {
               
                if (is_array($info2))
                {
                    foreach ($info2 as $key3=>$info3)
                    {
                        if (is_array($info3))
                        {
                           
                            foreach ($info3 as $info4)
                            {
                                echo $info4;
                            }
                           
                        }
                        else
                        {
                            if($key3=='tipo')
                            {
                                echo "tipo:".$info3."<br>";
                            }
                               
                            else
                            {
                                echo "id_habitacion:".$info3."<br>";
                            }
                        }
                    }
                    echo "<br>";
                }
                else
                {
                    echo "nada: ".$info2 . "<br>";
                   
                }

                echo "<br>";
            }
            echo "<br>";
        }
    }

    function recorro($matriz)
    {

        foreach ($matriz as $key => $value)
        {

            if (is_array($value))
            {
                //si es un array sigo recorriendo
                echo 'key:' . $key;
                echo '<br>';

                /* if ($key == '0')
                  {
                  echo '<div style="background-color: red">';
                  $this->recorro($value);
                  echo "<div>";
                  }
                  elseif ($key == 'array2')
                  {
                  echo '<div style="background-color: green">';
                  $this->recorro($value);
                  echo "<div>";
                  }
                  elseif ($key == 'array3')
                  {
                  echo '<div style="background-color: blue">';
                  $this->recorro($value);
                  echo "<div>";
                  } */
            }
            else
            {
                //si es un elemento lo muestro
                echo $key . ': ' . $value;
                echo '<br>';
            }
        }
    }
    
    function prueba2()
    {
        $query = "
                   
                    select ah.ID_Alojamiento as a, ah.ID_Habitacion as b, cal_calendario.fecha as c  
                    from
                    cal_calendario
                    inner join alojamientos_habitaciones as ah
                    on
                    cal_calendario.id_habitacion=ah.ID_Habitacion
                    where
                    fecha>'2013-01-01' and fecha<'2013-01-016'
                ";

        $rows_count = $this->db->query($query);
        $rows = $rows_count->result_array();
        
        foreach($rows as $var)
        {
            
        }
    }

}
